package com.jwd.avmanager.util.DateUtils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
/**
 * @author swx
 * @discrption  日期时间处理工具类
 */
public class TimeJudgeUtils {

    public static String strData(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = formatter.format(date);
        System.out.println(formattedDate);
        return formattedDate;
//
    }

    public static Date strData(String dateStr){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }




    public static String getNowDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
    //起始时间和截止时间比较,如果为ture表示到达了截止时间
    public static boolean judgeTime(String startTime,String endTime){
        boolean timeJudge = false;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date startDate=df.parse(startTime);
            Date endDate=df.parse(endTime);
            if(startDate.getTime()>endDate.getTime()){
                timeJudge=true;
            }else{
                timeJudge=false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return timeJudge;
    }
    //给定一个截止时间,默认当前时间为起始时间
    public static boolean judgeIsEndTime(String endTime){
        boolean timeJudge = false;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date startDate=df.parse(getNowDate());
            Date endDate=df.parse(endTime);
            if(startDate.getTime()>endDate.getTime()){
                timeJudge=true;
            }else{
                timeJudge=false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return timeJudge;
    }
 
    //返回指定时间加指定天后的结果时间
    public static  String addDayTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.DATE,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定月后的结果时间
    public static  String addMonthTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MONTH,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定年后的结果时间
    public static  String addYearTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.YEAR,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定小时后的结果时间
    public static  String addHourTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.HOUR_OF_DAY,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定分钟后的结果时间
    public static  String addMinuteTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MINUTE,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定秒后的结果时间
    public static  String addSecondTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.SECOND,n);
        return  sdf.format(cd.getTime());
    }
 
    //返回当前时间加指定天后的结果时间
    public static  String addNowDayTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.DATE,n);
        return  sdf.format(cd.getTime());
    }

    //返回当前时间加指定天后的结果时间
    public static  String addNowDayTime(int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.DATE,n);
        return  sdf.format(cd.getTime());
    }

    //返回当前时间加指定月后的结果时间
    public static  String addNowMonthTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MONTH,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定年后的结果时间
    public static  String addNowYearTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.YEAR,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定小时后的结果时间
    public static  String addNowHourTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.HOUR_OF_DAY,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定分钟后的结果时间
    public static  String addNowMinuteTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MINUTE,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定秒后的结果时间
    public static  String addNowSecondTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.SECOND,n);
        return  sdf.format(cd.getTime());
    }
    /***
     * @comments 计算两个时间的时间差
     * @param strTime1
     * @param strTime2
     * @return
     */
    public   static  Long  getTimeDifference(String strTime1, String strTime2) {
        long result = 0;
        //格式日期格式，在此我用的是"2018-01-24 19:49:50"这种格式
        //可以更改为自己使用的格式，例如：yyyy/MM/dd HH:mm:ss 。。。
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date now = df.parse(strTime1);
            Date date=df.parse(strTime2);
            long l=now.getTime()-date.getTime();       //获取时间差
            long day=l/(24*60*60*1000);
            long hour=(l/(60*60*1000)-day*24);
            long min = l / (1000 * 60);
            long s=(l/1000-day*24*60*60-hour*60*60-min*60);
            result= min;
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 格式化日期
     *
     * @param dateStr String 字符型日期
     * @param format  String 格式
     * @return Date 日期
     */
    public static Date parseDate(String dateStr, String format) {
        Date date = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(format);
            date = dateFormat.parse(dateStr);
        } catch (Exception e) {

        }
        return date;
    }
 
    public static void main(String[] args) {
        String nowDate = TimeJudgeUtils.getNowDate();
        String addNowDayTime = TimeJudgeUtils.addNowDayTime(5)+" 23:59:59";
        System.out.println(nowDate);
        System.out.println(addNowDayTime);

        boolean b = judgeTime("2023-11-29 15:06:05", "2023-11-29 15:06:04");
        System.out.println(b);
    }

}